<template>
  <div class="login_container">
    <div class="login_box">
      <div class="avatar_box">
        <img src="../assets/logo.png" alt="">
      </div>
      <div class="login_form">
        <el-form ref="loginFormRef" :model="loginForm" :rules="rules">
          <el-form-item prop="username">
            <el-input v-model="loginForm.username" prefix-icon="el-icon-user" placeholder="账号"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input v-model="loginForm.password" prefix-icon="el-icon-key" type="password"
                      placeholder="密码"></el-input>
          </el-form-item>
          <el-form-item class="btn">
            <el-button type="primary" @click="login">登陆</el-button>
            <el-button type="info" @click="reset">重置</el-button>
          </el-form-item>
          <el-switch
              style="display: block"
              v-model="isStudent"
              active-color="#13ce66"
              inactive-color="#ff4949"
              active-text="学生"
              inactive-text="老师"
          >
          </el-switch>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import axios from "axios";

export default {
  name: "Login",

  data() {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      rules: {
        //验证username合法性
        username: [
          {required: true, message: '请输入账号', trigger: 'blur'},
          {min: 4, max: 4, message: '请输入4位账号', trigger: 'blur'}
        ],
        //验证password合法性
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 4, max: 4, message: '请输入4位的密码', trigger: 'blur'}
        ]
      },
      isStudent: true

    }
  },
  methods: {
    reset() {
      this.$refs.loginFormRef.resetFields()
    },

    //todo 完善后端后对这里修改登陆验证
    login() {
      this.$refs.loginFormRef.validate(valid => {
        //如果表单数据通过验证
        if (valid) {
          console.log("登陆成功")
          const username = this.loginForm.username;
          const password = this.loginForm.password
          if (this.isStudent) {
            console.log('18岁是学生Dase')
            axios
                .get(`student/login/${username}/${password}`)
                .then(response => {
                  console.log('取得id为')
                  console.log(response.data)
                  const id=response.data
                  this.$store.commit("setStudentId",id)
                  window.sessionStorage.setItem('token', id)
                  this.$router.push({path:`/student/${id}`})
                })
                .catch(e => {
                  console.log(e)
                })
          } else {
            console.log('25岁是老师daka')
            axios
                .get(`teacher/login/${username}/${password}`)
                .then(response => {
                  console.log('取得id为')
                  console.log(response.data)
                  const id=response.data
                  this.$store.commit("setTeacherId",id)

                  window.sessionStorage.setItem('token', id)
                  this.$router.push({path:`/teacher/${id}`})
                })
                .catch(e => {
                  console.log(e)
                })
          }


        } else {
          this.$message.error('用户名或密码错误')
        }
      })
    }
  }
}
</script>

<style scoped>
.login_container {
  height: 100%;
  background-color: #7f8c8d;
}

.login_box {
  width: 450px;
  /*设置居中 */
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  border-radius: 20px;
  background: #e1dfdf;
  box-shadow: -20px -20px 51px #717d7d,
  20px 20px 51px #8d9b9d;
}

.login_box .avatar_box {
  height: 130px;
  width: 130px;
  border: 1px solid #dddddd;
  border-radius: 50%;
  padding: 10px;
  box-shadow: 0 0 10px #dddddd;
  background-color: #ecf0f1;

  position: relative;
  left: 50%;
  transform: translate(-50%, -55%);
}

.avatar_box img {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background-color: #dddddd;
}

.login_form {
  padding: 20px;
  position: relative;
  transform: translateY(-15%);
}

.btn {
  display: flex;
  justify-content: center;
}
</style>